// JavaScript Document
$(document).ready(function(){
    $("#checkall").click(function(){
        checkStatus=this.checked;
        $(".check").each(function(){
            this.checked=checkStatus;
        });
    });
	
    $("#cancel").click(function(){
        $(".check").each(function(){
            this.checked=false;
        });
        document.getElementById('checkall').checked = false
    });
});

//start listbox function
function listbox_moveOptions(sourceID, destID) {

    var src = document.getElementById(sourceID);
    var dest = document.getElementById(destID);
    var totalOptions = src.options.length;
    for(var count=0; count < totalOptions; count++) {
        if(src.options[count].selected == true) {
            var option = src.options[count];
            var newOption = document.createElement("option");
            newOption.value = option.value;
            newOption.text = option.text;
            newOption.selected = true;
                
               
            try {
                dest.add(newOption, null); //Standard
                src.remove(count, null);
            //src.options[count].selected = false;
            }catch(error) {
                dest.add(newOption); // IE only
                src.remove(count);
            //src.options[count].selected = false;
            }
            count--;
        }
    }

}
	
function listbox_selectall(listID, isSelect) {
    var listbox = document.getElementById(listID);
    for(var count=0; count < listbox.options.length; count++) {
        listbox.options[count].selected = true;
    }
}


function hasOptions(obj){
    if(obj!=null && obj.options!=null){
        return true;
    }return false;
}
function swapOptions(obj,i,j){
    var o = obj.options;
    var i_selected = o[i].selected;
    var j_selected = o[j].selected;
    var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
    if(o[i].id!=''){
        temp.id = o[i].id;
        temp.title = o[i].title;
    }
    var temp2 = new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
    if(o[j].id!=''){
        temp2.id = o[j].id;
        temp2.title = o[j].title;
    }
    o[i] = temp2;
    o[j] = temp;
    o[i].selected = j_selected;
    o[j].selected = i_selected;

}
function moveOptionUp(obj){
    if (!hasOptions(obj)) {
        return;
    }
    for (i = 0; i < obj.options.length; i++) {
        if (obj.options[i].selected) {
            if (i != 0 && !obj.options[i - 1].selected) {
                swapOptions(obj, i, i - 1);
                obj.options[i - 1].selected = true;
            }
        }
    }
}
function moveOptionDown(obj){
    if (!hasOptions(obj)) {
        return;
    }
    for (i = obj.options.length - 1; i >= 0; i--) {
        if (obj.options[i].selected) {
            if (i != (obj.options.length - 1) && !obj.options[i + 1].selected) {
                swapOptions(obj, i, i + 1);
                obj.options[i + 1].selected = true;
            }
        }
    }
}
//end listbox function

function redirect_url(url)
{
    window.location = url;
}
function confirm_get(mess,url)
{
	if(confirm(mess)){
        window.location.href = url;
    }
}
function confirm_post(mess,form_id)
{
        if(confirm(mess)){
            $("#"+form_id).submit();
        }
    
}
function confirm_cutom(message, callback) {
    $('#confirm').modal({
        closeHTML:"<a href='#' title='Close' class='modal-close'>x</a>",
        position: ["20%",],
        overlayId:'confirm-overlay',
        containerId:'confirm-container',
        onShow: function (dialog) {
            $('.message', dialog.data[0]).append(message);

            // if the user clicks "yes"
            $('.yes', dialog.data[0]).click(function () {
                // call the callback
                if ($.isFunction(callback)) {
                    callback.apply();
                }
                // close the dialog
                $.modal.close();
            });
        }
    });
}

function popupImages(parameter){
    var urlImage = ADMIN_URL+'/index.php?module=images'+parameter;
    var width=900;
    var height=650;
    var left = (screen.width-width)/2;
    var top = (screen.height-height)/2;
    var settings = "width="+width+",height="+height+",scrollbars=yes,top="+top+",left="+left;
    window.open(urlImage,'mywindow' ,settings);
}

window.onerror=function(msg, url, linenumber){
    var logerror='Error message: ' + msg + '. Url: ' + url + 'Line Number: ' + linenumber
    alert(logerror)
    return true
}

function in_array(needle,haystack,argStrict){
    var key='',strict=!!argStrict;if(strict){
        for(key in haystack){
            if(haystack[key]===needle){
                return true;
            }
        }
    }else{
        for(key in haystack){
            if(haystack[key]==needle){
                return true;
            }
        }
    }
    return false;
}
function strpos(haystack,needle,offset){
    var i=(haystack+'').indexOf(needle,(offset?offset:0));
    return i===-1?false:i;
}

function rand(min,max){
    var argc=arguments.length;if(argc===0){
        min=0;max=2147483647;
    }else if(argc===1){
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random()*(max-min+1))+min;
}













// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
	cal.hide();			// hide the calendar

	// don't check mousedown on document anymore (used to be able to hide the
	// calendar when someone clicks outside it, see the showCalendar function).
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	// FIXME: allow end-user to click some link without closing the
	// calendar.  Good to see real-time stylesheet change :)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		// calls closeHandler which should hide the calendar.
		calendar.callCloseHandler(); Calendar.stopEvent(ev);
	}
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
	function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide();		// hide the existing calendar
		calendar.parseDate(el.value); // set it to a new date
	} else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;		// remember the calendar in the global
		cal.setRange(1990, 2070);	// min/max year allowed
                cal.dateFormat=('y-mm-dd');
		calendar.create();		// create a popup calendar
		calendar.parseDate(el.value); // set it to a new date
	}
	calendar.sel = el;		// inform it about the input field in use
	calendar.showAtElement(el);	// show the calendar next to the input field

	// catch mousedown on the document
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}
